package cn.tedu.ivos.dict.controller;
import cn.tedu.ivos.base.handler.JsonResult;
import cn.tedu.ivos.dict.pojo.dto.DictItemQueryParam;
import cn.tedu.ivos.dict.pojo.dto.DictItemSaveParam;
import cn.tedu.ivos.dict.pojo.vo.DictItemVO;
import cn.tedu.ivos.dict.service.DictItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/dict/item")
public class DictItemController {
    @Autowired
    private DictItemService dictItemService;

    @GetMapping("select")
    public JsonResult selectDictItem(DictItemQueryParam dictItemQuery) {
        log.debug("查询字典项：dictItemQuery={}", dictItemQuery);
        List<DictItemVO> list =  dictItemService.selectDictItem(dictItemQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    public JsonResult saveDictItem(DictItemSaveParam dictItemSaveParam) {
        log.debug("保存字典项：dictItemSaveParam={}", dictItemSaveParam);
        dictItemService.saveDictItem(dictItemSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select/{code}")
    public JsonResult selectDictItemByCode(@PathVariable String code){
        log.debug("根据字典code查询对应的所有字典项：code={}", code);
        List<DictItemVO> list = dictItemService.selectDictItemByCode(code);
        return JsonResult.ok(list);
    }

    @PostMapping("delete/{id}")
    public JsonResult deleteDictItem(@PathVariable Long id) {
        log.debug("删除字典项:id={}",id);
        dictItemService.deleteDictItem(id);
        return JsonResult.ok();
    }
}